﻿using Ducu.SRV.ServiceInteface;
using Ducu.SRV.ViewModel.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ducu.UI.Responsible.Controllers
{
    public class LogController : BaseController
    {
        #region Constructor

        private IRegisterService _registerSevice;
        private ILogService _logService;
        public LogController(IRegisterService registerService, ILogService logService)
        {
            _registerSevice = registerService;
            _logService = logService;
        }

        #endregion

        #region URL: /Log/On

        public ActionResult On()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult On(OnModel model, bool? remember)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            //encode the UserName
            model.UserName = Server.HtmlEncode(model.UserName);

            //check whether the username is exist by its password
            if (!_registerSevice.IsUserNameExist(model.UserName))
            {
                ModelState.AddModelError("UserName", "* 无法找到该用户名");
                return View(model);
            }

            if (!_logService.HasPassed(model))
            {
                ModelState.AddModelError("Password", "* 用户名或密码错误");
                return View(model);
            }

            int? days = null;
            if (remember.HasValue)
            {
                days = 60;
            }
            _logService.On(model, days);

            return Prepage.Return();
        }

        #endregion

        #region ULR: /Log/Off

        public ActionResult Off()
        {
            _logService.Off();
            return Prepage.Return();
        }

        #endregion

    }
}